﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using PMC.Structs;

namespace VacationExtension
{
    public partial class VacationExtensionTabPage : TabPage
    {
        private VacationExtension context;
        private BindingList<Website> excludedWebsites; 

        public VacationExtensionTabPage(IBindingList list, VacationExtension context)
        {
            this.context = context;
            InitializeComponent();
            lbWebsites.SelectionMode = SelectionMode.MultiExtended;

            var excludedWebsites = new List<Website>(Website.Websites);
            excludedWebsites.RemoveAll(list.Contains);
            this.excludedWebsites = new BindingList<Website>(excludedWebsites);

            Text = "Vacation";
            lbWebsites.DataSource = list;
            lbWebsites.DisplayMember = "Name";
            Dock = DockStyle.Fill;
            mcDate.MaxSelectionCount = 1;
            mcDate.MinDate = DateTime.Now;
        }

        private void btnVacation_Click(object sender, EventArgs e)
        {
            context.Process(mcDate.SelectionRange.Start - TimeSpan.FromDays(3)); //Should be next
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var websites = lbWebsites.SelectedItems.Cast<Website>().ToList();
            foreach (Website website in websites)
            {
                excludedWebsites.Add(website);
                ((IBindingList) lbWebsites.DataSource).Remove(website);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (var dialog = new VacationWebsitesAddForm(excludedWebsites))
            {
                dialog.ShowDialog();

                if (dialog.SelectedWebsite != null)
                    ((IBindingList) lbWebsites.DataSource).Add(dialog.SelectedWebsite);
            }
        }
    }
}
